// code-order/pages/invoices.js
import {
    findInvoiceTab,
    getOrders
} from "@src/api/invoice";
import router from '@src/router'
import invoiceBehavior from "./invoiceBehavior";
Page({
    behaviors: [invoiceBehavior],
    data: {
        isChecked: false,
        orderData: [],
        contentText: {
            '赠送会员': '赠送会员订单',
            '会员充值': '会员充值订单',
            '会员续费': '会员续费订单',
            '会员开卡': '会员开卡订单',
            '预订': '会员预订订单',
            '点餐': '会员点餐订单',
        },
        sysInstanceIndex: 1,
        sysInstanceTotalPages: 0,
        money: null,
        moneyArr: [],
        goPath: {
            5: 'sp-mall/pages/order-detail/index', //商城
            17: 'sp-reservation/pages/reservation_order_detail/main', //预约
            7: 'sp-booking/pages/orderDetail/main', // 预定
            6: 'sp-catering/pages/orderdetails/main', // 点餐
            9: 'sp-member/pages/sendGiftDetail/main', //赠送会员
            2: 'sp-member/pages/balanceListDetail/main' // 会员充值 开卡 续费
        },

        flag: true
    },

    radioCheck(e) {
        const {
            index,
            ischecked
        } = e.currentTarget.dataset
        const {
            slots,
            currentIndex
        } = this.data
        let orderData = slots[currentIndex].orderData.slice()
        let money = null
        let isChecked = false
        this.data.moneyArr = []
        orderData.forEach((item, idx) => {
            item.isChecked = false // 预留往后一次多选开多张发票时去掉这句就行且orderNo以数组的方式接收
            idx === index && (item.isChecked = !ischecked, isChecked = item.isChecked)
            item.isChecked && this.data.moneyArr.push(item.receiptAmt)
            money = this.data.moneyArr.length && this.data.moneyArr.reduce((a, b) => {
                return a + b
            })
        })
        this.setData({
            ['slots[' + currentIndex + '].orderData']: orderData,
            isChecked,
            money
        })
    },
    selectTab(pageNum, pageSize, orderType) {
        return new Promise((resolve, reject) => {
            getOrders({
                pageNum,
                pageSize,
                orderType
            }).then(res => {
                this.curOrderType = orderType
                resolve(res)
            }).catch(err => {
                reject(err)
                wx.showToast({
                    title: err.msg,
                    icon: 'none',
                    duration: 2000
                })
            })
        })
    },
    async onShow(options) {
        const pages = getCurrentPages()
        const currPage = pages[pages.length - 1]
        let openFapiaoStatus = currPage.data.openFapiaoStatus
        // console.log('接收开票状态 currPage', openFapiaoStatus)
        if (openFapiaoStatus) {
            const orderType = this.curOrderType ? this.curOrderType : await this.findInvoiceTab()
            this.tabChangeApi(orderType, openFapiaoStatus)
        }
    },
    async onLoad(options) {
        const orderType = await this.findInvoiceTab()
        this.tabChangeApi(orderType)
    },
    findInvoiceTab() {
        return new Promise(resolve => {
            findInvoiceTab().then(res => {
                if (res.code === 0) {
                    const slotsFilter = this.data.slots.slice()
                    const slots = slotsFilter.filter(item => {
                        item.flag = res.data[item.responseSts]
                        return item.flag === 1
                    })
                    this.setData({
                        slots,
                        loading: false
                    })
                    resolve(slots[0].orderType)
                }
            })
        })
    },
    goMyOrder(e) {
        const {
            slots,
            goPath,
            currentIndex
        } = this.data
        const navWChoose = slots[currentIndex].goMyOrder // 预留后期可能跳列表的情况
        const {
            index
        } = e.currentTarget.dataset

        const {
            orderNo,
            shopId,
            receiveCode,
            fapiaoChargeDto
        } = slots[currentIndex].orderData[index]
        const path = goPath[slots[currentIndex].orderType]

        if (slots[currentIndex].orderType === 2) {
            router.push({
                path,
                success: (res) => {
                    res.eventChannel.emit('item', fapiaoChargeDto)
                }
            })
        } else {

            router.push({
                path,
                params: {
                    orderNo,
                    shopId,
                    receiveCode
                }
            })
        }

    },
    goHistory() {
        router.push({
            path: 'code-order/pages/invoiceHistory/main',
            params: {
                currentIndex: this.data.currentIndex
            }
        })
    },
    goInvoicing(e) {
        if (!this.data.isChecked) return
        const {
            slots,
            currentIndex
        } = this.data
        let shopId = null
        let businessId = null
        let orderNo = []
        let goodsTotNum = null
        slots[currentIndex].orderData.filter(item => {
            return item.isChecked
        }).forEach(ele => {
            shopId = ele.shopId
            businessId = ele.businessId
            goodsTotNum = ele.goodsTotNum
            orderNo.push(ele.orderNo)
        })
        router.push({
            path: 'code-order/pages/invoicing/main',
            params: {
                money: this.data.money,
                businessId,
                shopId,
                goodsTotNum,
                orderNo: orderNo.join()
            }
        })
    },
    goInvoiceExplain() {
        router.push({
            path: 'code-order/pages/invoiceExplain/main',
        })
    },

})